package com.fcgzs.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fcgzs.springboot.mapper.CheckitemMapper;
import com.fcgzs.springboot.pojo.Checkitem;
import com.fcgzs.springboot.service.CheckitemService;
import com.fcgzs.springboot.util.MessageConstant;
import com.fcgzs.springboot.util.PageResult;
import com.fcgzs.springboot.util.QueryPageBean;
import com.fcgzs.springboot.util.Result;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Service //注意是dubbo类型
public class CheckitemServiceImpl implements CheckitemService {

    //从容器获取mapper
    @Resource
    private CheckitemMapper checkitemMapper;

    //添加、编辑检查项
    @Override
    public Result addCheckitemInfo(Checkitem checkitem) {
        boolean flag = false;

        if(checkitem.getId()!=null){
            flag = checkitemMapper.updateById(checkitem) > 0;
        }else {
            flag = checkitemMapper.insert(checkitem) > 0;
        }
        Result result = null;
        if(flag){
            return result = new Result(flag,MessageConstant.ADD_CHECKITEM_SUCCESS);
        }else {
            return result = new Result(flag,MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }
    //检查项：弹出编辑窗口时回显数据
    @Override
    public Result getOneCheckitemInfo(Integer id) {
        return new Result(true,"",checkitemMapper.selectById(id));
    }

    //检查项：全表分页、模糊查询
    @Override
    public PageResult queryPageInfo(QueryPageBean queryPageBean) {
        Page<Checkitem> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Checkitem> queryWrapper = new QueryWrapper<>();
        if(queryPageBean.getQueryString()!=null&&queryPageBean.getQueryString().length()>0){
            queryWrapper.like("code",queryPageBean.getQueryString());
            queryWrapper.or();
            queryWrapper.like("name",queryPageBean.getQueryString());
        }
        Page<Checkitem> checkitemPage = checkitemMapper.selectPage(page, queryWrapper);
        return new PageResult(checkitemPage.getTotal(),checkitemPage.getRecords());
    }

    //检查项：删除单个
    @Override
    public Result deleteCheckitemInfo(Integer id) {
        boolean flag = checkitemMapper.deleteById(id)>0;
        Result result = null;
        if(flag){
            return result = new Result(flag, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }else{
            return result = new Result(flag, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    //检查组：获取所有检查项
    @Override
    public Result getAllCheckitemInfo() {
        return new Result(true,null,checkitemMapper.selectList(null));
    }
}
